<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/24 0024
 * Time: 13:20
 */

namespace app\api\controller\product;

use app\api\model\ProductReview;
use app\Validate\PageMustValidate;
use app\common\model\ProductOut as ProductOutService;
use think\Controller;
use think\Request;

/**
 * 清仓业务端方法类
 * Class ProductOut
 * @package app\admin\controller\shop
 */
class ProductOut extends Controller{
    /**
     * 获取清仓商品列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     * @throws \think\exception\DbException
     */
    public function getProductOutList(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();

        /*
        Select
        om_product_out.id,
        om_product_out.cover_img,
        om_product_out.out_name,
        om_product_out.batch_num,
        om_product_out.out_price,
        om_product.product_price_one,
        DATE_FORMAT(FROM_UNIXTIME(om_product_out.start_time),'%m.%d') as start_time,
        DATE_FORMAT(FROM_UNIXTIME(om_product_out.end_time),'%m.%d')as end_time,
        om_warehouse_general_sku.stock
        from om_product_out
        inner join om_product on om_product_out.product_id = om_product.id
        inner join om_warehouse_general_sku on om_product_out.general_sku_id = om_warehouse_general_sku.id
        where om_product_out.is_show = 1 and om_product.is_publish = 2
        order by end_time desc*/
        $productOutService = new ProductOutService();
        $field = "om_product_out.id,
        om_product_out.cover_img,
        om_product_out.out_name,
        om_product_out.batch_num,
        om_product_out.out_price,
        om_product.product_price_one,
        DATE_FORMAT(FROM_UNIXTIME(om_product_out.start_time),'%m.%d') as start_time,
        DATE_FORMAT(FROM_UNIXTIME(om_product_out.end_time),'%m.%d')as end_time,
        om_warehouse_general_sku.stock";
        $where["om_product_out.is_show"] = 1;
        $where["om_product.is_publish"] = 2;
        $list = $productOutService->field($field)->where($where)
            ->join("om_product","om_product_out.product_id = om_product.id")
            ->join("om_warehouse_general_sku","om_product_out.general_sku_id = om_warehouse_general_sku.id")
            ->whereNull("om_product_out.delete_time")
            ->order("start_time","desc")
            ->paginate($post['page_number'], false, ['page' => $post['page']])->toArray();
        return writeJson(200,$list);
    }

    /**
     * 获取清仓商品详情
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProductOut(Request $request){
        $productOutService = new ProductOutService();
        $productReviewService = new ProductReview();
        $post = $request->post();
        if(!isset($post["id"])){
            return writeJson(200,"缺少商品id","缺少商品id",10000);
        }

        /*Select
        om_product_out.id,
        om_product_out.cover_img,
        om_product_out.out_name,
        om_product_out.batch_num,
        om_product_out.out_price,
        om_product.product_price_one,
        FROM_UNIXTIME(om_product_out.start_time) as start_time,
        FROM_UNIXTIME(om_product_out.end_time) as end_time,
        om_warehouse_general_sku.stock,
        om_product.sku_remark,
        om_product.description,
        om_product.img_path
        from om_product_out
        inner join om_product on om_product_out.product_id = om_product.id
        inner join om_warehouse_general_sku on om_product_out.general_sku_id = om_warehouse_general_sku.id
        where om_product_out.is_show = 1 and om_product.is_publish = 2
        */
        $field = 'om_product_out.id,om_product_out.cover_img,om_product_out.out_name,om_product_out.batch_num,
        om_product_out.out_price,om_product.product_price_one,FROM_UNIXTIME(om_product_out.start_time) as start_time,
        FROM_UNIXTIME(om_product_out.end_time) as end_time,om_warehouse_general_sku.stock,om_product.sku_remark,
        om_product.description,om_product.img_path,om_product_out.product_id';
        $where["om_product_out.id"] = $post["id"];
        $where["om_product_out.is_show"] = 1;
        $where["om_product.is_publish"] = 2;
        $model = $productOutService->field($field)
            ->join("om_product","om_product_out.product_id = om_product.id")
            ->join("om_warehouse_general_sku","om_product_out.general_sku_id = om_warehouse_general_sku.id")
            ->whereNull("om_product_out.delete_time")
            ->where($where)->find();
        if($model == null){
            return writeJson(200,"当前清仓商品不存在","当前清仓商品不存在",10000);
        }
        //解码
        $model['img_path'] = serializeMysql($model['img_path'],1);
        $model["description"] = (new \app\api\model\Product())->getProductDescription($model["product_id"]);

        //查询当前商品评论
        $reviewWhere["type"] = 2;
        $reviewWhere["order_id"] = $model["id"];

        /**
         * select
        om_product_review.content,phone,
        FROM_UNIXTIME(om_product_review.create_time) as create_time
        from om_product_review
        inner join om_user on om_product_review.user_id = om_user.id
        where type = 2 and order_id = 2
         */
        $list = $productReviewService
            ->field("om_product_review.content, phone, om_product_review.create_time")
            ->join("om_user","om_product_review.user_id = om_user.id")
            ->where($reviewWhere)
            ->whereNull("om_product_review.delete_time")
            ->order("create_time desc")->limit(5)->select()->toArray();

        //组合
        $result = array(
            "model"=>$model->toArray(),
            "review"=>$list
        );
        return writeJson(200,$result);
    }
}